/*
 * 文件名：MD5Util.java
 * 版权：Copyright 2006-2011 AsiaInfo Tech. Co. Ltd. All Rights Reserved. 
 * 描述： MD5Util.java
 * 修改人：Administrator
 * 修改时间：2011-8-25
 * 修改内容：新增
 */
package com.fengxing.ams.pub.util;

import java.security.MessageDigest;

import com.fengxing.ams.pub.UtilityException;

/**
 * MD5加密类
 * <p>
 * MD5加密类
 * 
 * <pre>
 * </pre>
 * 
 * @author ebusu
 * @version CTMS V100R001 2011-8-25
 * @since CTMS V100R001C01
 */
public class MD5Util
{
    /**
     * 
     * MD5加密
     * 
     * @param
     * @param password
     *            需要加密的密码
     * @return 32位加密
     */
    public static String md5(String password)
    {
        try
        {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(password.getBytes());
            byte b[] = md.digest();

            return new String(encode(b));

        }
        catch (Exception e)
        {
            throw new UtilityException(e);
        }
    }

    private static char[] encode(byte[] bytes)
    {
        final int nBytes = bytes.length;
        char[] result = new char[2 * nBytes];

        int j = 0;
        for (int i = 0; i < nBytes; i++)
        {
            // Char for top 4 bits
            result[j++] = HEX[(0xF0 & bytes[i]) >>> 4];
            // Bottom 4
            result[j++] = HEX[(0x0F & bytes[i])];
        }

        return result;
    }

    private static final char[] HEX = {'0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    public static void main(String args[])
    {
    	System.out.println(MD5Util.md5("abc123yssj"));
        
    }

}
